/**
 * Created by zhangzhihao on 2016/12/06 11:00;
 */
var vm = new Vue({
    el: '#main',
    data: {
        menuNodes: [],
        zTree: {},
        setting: {},
        role:{
            roleNo:App.getParam("roleNo")
        },
        roleMenus:{
			datas:[],
			object:{
				roleNo:App.getParam("roleNo")
			}
		},
		roleTypes:[],
	    agentCates:[]
    },
    created: function () {
    	this.queryRolesType();
        this.queryAgentCate();
    	
        this.setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "dataAuthNo",
                    pIdKey: "parentDataAuth"
                },
                key: {
                    name: "name"
                }
            }
        };
        
        this.getRoleInfo();
        this.getMenuTree();
        
    },
    
    methods: {
        //返回上一页的点击事件的函数
        goBtn: function () {
            //不保留
            history.back(-1);
            //保留
//			history.go(-1);
        },
        queryRolesType:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("queryRolesTypeURL"),
    			success:function(data){
    				vm.roleTypes = data;
				}
			});
        },
        queryAgentCate:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("queryAgentCateURL"),
    			success:function(data){
    				vm.agentCates = data;
    				vm.getRoleInfo();
				}
			});
        },
        getRoleInfo:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("getRolesInfoURL") + "?roleNo=" + this.role.roleNo,
    			success:function(data){
    				vm.role = data;
                    selectSet("roleType",data.roleType);
                    selectSet("agentCate",data.agentCate);
    			}
    		});
        },
        getMenuTree:function(){
        	App.ajax({
    			type:"GET",
    			url:App.getApiUrl("queryRolesMenuURL") + "?roleNo=" + this.role.roleNo,
    			success:function(data){
    				vm.menuNodes = data;

                    $.fn.zTree.init($("#menuTree"), vm.setting, vm.menuNodes);
                    vm.zTree = $.fn.zTree.getZTreeObj("menuTree");
    			}
    		});
        },
        saveRoleMenu:function(){
        	this.roleMenus.datas = [];
        	var nodes = this.zTree.getCheckedNodes(true);
        	for(var i=0;i<nodes.length;i++){
        		//alert(JSON.stringify(nodes[i].check_Child_State));
        		this.roleMenus.datas.push({
        			dataAuthNo:nodes[i].dataAuthNo,
        			chkDisabled:nodes[i].chkDisabled
        		});
        	}
        	App.ajax({
    			type:"POST",
    			url:App.getApiUrl("mdfRolesMenuURL"),
    			data: this.roleMenus,
    			success:function(data){
    				parent.showAlert(data.msg);
    			}
    		});
    	}
    }
});